
@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    @*<link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">*@
    <link href="~/layuimini/lib/layui-v2.6.3/css/layui.css" rel="stylesheet" />
    @*<link rel="stylesheet" href="../css/public.css" media="all">*@
    <link href="~/layuimini/css/public.css" rel="stylesheet" />

    <style>
        .layui-table-cell {
            height: auto !important;
            white-space: normal;
        }
    </style>
</head>
<body>
    <div class="layuimini-container">
        <div class="layuimini-main">

            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="">


                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">商品类型</label>
                                <div class="layui-input-inline">
                                    <select name="CategoryId" id="CategoryIdType">
                                        <option value="">选择商品类型</option>

                                    </select>
                                </div>
                            </div>




                            <div class="layui-inline">
                                <label class="layui-form-label">商品名称</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="PhoneNum" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>

            @*//商家可看*@
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
                </div>
            </script>

            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


            @*管理员可见*@
            <script type="text/html" id="currentTableBar1">
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="details">查看详情</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="shoppingcart">加入购物车</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="playcommodity">购买</a>
            </script>


            @*用户可见*@
            <script type="text/html" id="currentTableBar2">
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="playcommodity">购买</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="shoppingcart">加入购物车</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="details">查看详情</a>
            </script>

            @*商家可见*@
        <script type="text/html" id="currentTableBar3">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="details">查看详情</a>
        </script>




        </div>
    </div>
    @*<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>*@
    <script src="~/layuimini/lib/layui-v2.6.3/layui.js"></script>

    <script type="text/html" id="imgtmp">
        <img src="{{d.commodityImg}}"  style="width:108px;height:90px" />
    </script>



    <script>


        var tableIns;

        var commodityId;


        //table实例
        var table;
        var form;
        var $;

        layui.use(['form', 'table'], function () {
            $ = layui.jquery;
            form = layui.form;
            table = layui.table;


            //异步获取商品下拉选数据集合
            $.ajax({
                url: 'http://localhost:31861/api/CommodityInfo/CommodityTypeOption',
                type: 'get',
                success: function (res) {

                    if (res.isSuccess) {
                        if (res.datas) {
                            //赋值商品下拉选
                            for (var i = 0; i < res.datas.length; i++) {
                                $('#CategoryIdType').append('<option value="' + res.datas[i].id + '">' + res.datas[i].categoryName + '</option>');
                            }

                            form.render('select'); //刷新select选择框渲染
                        }
                    }
                }
            });




            $.ajax({
                url: 'http://localhost:31861/api/UserInfo/IsUserRole',
                type: 'get',
                xhrFields: {
                    withCredentials: true
                },
                success: function (res) {
                    for (var i = 0; i < res.datas.length; i++) {
                        if (res.datas[i].roleName=="用户") {
                            initView("用户")
                        } else if (res.datas[i].roleName == "商家") {
                            initView("商家")

                        } else if (res.datas[i].roleName == "配送员") {
                            initView("配送员")

                        } else if (res.datas[i].roleName == "系统管理员") {
                            initView("系统管理员")

                        }
                    }
                }
            });





            //初始化页面
            function initView(state) {
                var arr;
                var toolbar = '';
                if (state == "系统管理员") {
                    arr = [[
                        { type: "checkbox", width: 50, style: "height:100px" },
                        { field: 'id', title: 'ID', hide: true, style: "height:100px" },
                        { field: 'commodityName', width: 120, title: '商品名称' },
                        { field: 'categoryId', width: 100, title: '商品类型' },
                        { field: 'price', minWidth: 80, title: '商品价格/件' },
                        { field: 'num', minWidth: 80, title: '商品数量' },
                        { field: 'commodityImg', minWidth: 120, title: '商品图片', align: "center", templet: "#imgtmp" },
                        { field: 'createTime', title: '加入购物车时间' },
                        { title: '操作', minWidth: 250, toolbar: '#currentTableBar1', align: "center", style: "height:100px" }
                    ]];

                    toolbar = '#toolbarDemo';

                } else if (state == "商家"){
                    arr = [[
                        { type: "checkbox", width: 50, style: "height:100px" },
                        { field: 'id', title: 'ID', hide: true, style: "height:100px" },
                        { field: 'commodityName', width: 120, title: '商品名称' },
                        { field: 'categoryId', width: 100, title: '商品类型' },
                        { field: 'price', minWidth: 80, title: '商品价格/件' },
                        { field: 'num', minWidth: 80, title: '商品数量' },
                        { field: 'commodityImg', minWidth: 120, title: '商品图片', align: "center", templet: "#imgtmp" },
                        { field: 'createTime', title: '加入购物车时间' },
                        { title: '操作', minWidth: 250, toolbar: '#currentTableBar3', align: "center", style: "height:100px" }
                    ]];

                    toolbar = '#toolbarDemo';
                } else  {
                    arr = [[
                        { type: "checkbox", width: 50, style: "height:100px" },
                        { field: 'id', title: 'ID', hide: true, style: "height:100px" },
                        { field: 'commodityName', width: 120, title: '商品名称' },
                        { field: 'categoryId', width: 100, title: '商品类型' },
                        { field: 'price', minWidth: 80, title: '商品价格/件' },
                        { field: 'num', minWidth: 80, title: '商品数量' },
                        { field: 'commodityImg', minWidth: 120, title: '商品图片', align: "center", templet: "#imgtmp" },
                        { field: 'createTime', title: '加入购物车时间' },
                        { title: '操作', minWidth: 250, toolbar: '#currentTableBar2', align: "center", style: "height:100px" }
                    ]];
                }



                //渲染table并且获取实例
                tableIns = table.render({
                    elem: '#currentTableId',
                    url: 'http://localhost:31861/api/CommodityInfo/CommodityInfoPageList',
                    toolbar: toolbar,
                    defaultToolbar: ['filter', 'exports', 'print', {
                        title: '提示',
                        layEvent: 'LAYTABLE_TIPS',
                        icon: 'layui-icon-tips'
                    }],
                    cols: arr,
                    limits: [5, 10, 15, 20, 25, 50],
                    limit: 5,
                    page: true,
                    skin: 'line'
                });



                // 监听搜索操作
                form.on('submit(data-search-btn)', function (data) {
                    var result = JSON.stringify(data.field);

                    tableIns.reload({
                        where: { //设定异步数据接口的额外参数，任意设
                            categoryId: data.field.CategoryId
                            , phoneNum: data.field.PhoneNum
                            //…
                        },
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });

                    return false;
                });

                /**
                 * toolbar监听事件
                 */
                table.on('toolbar(currentTableFilter)', function (obj) {
                    if (obj.event === 'add') {  // 监听添加操作
                        var index = layer.open({
                            title: '添加商品',
                            type: 2,
                            shade: 0.2,
                            maxmin: true,
                            shadeClose: true,
                            area: ['100%', '100%'],
                            content: '/CommodityInfo/AddCommodityInfoView'
                        });
                        $(window).on("resize", function () {
                            layer.full(index);
                        });
                    } else if (obj.event === 'delete') {  // 监听删除操作
                        var checkStatus = table.checkStatus('currentTableId');
                        var data = checkStatus.data;
                        //layer.alert(JSON.stringify(data));

                        console.log(data);
                        var ids = [];
                        for (var i = 0; i < data.length; i++) {
                            ids.push(data[i].id);
                        }

                        //发起请求批量删除用户
                        $.ajax({
                            url: 'http://localhost:31861/api/CommodityInfo/DeleteCommodityInfos',
                            type: 'post',
                            data: {
                                userInfoIds: ids
                            },
                            success: function (res) {
                                if (res.isSuccess) {
                                    //刷新表格
                                    reLoadTable();
                                }
                                //提示信息
                                layer.msg(res.msg);
                            }
                        });
                    }
                });

                //监听表格复选框选择
                table.on('checkbox(currentTableFilter)', function (obj) {
                    console.log(obj);
                });

                table.on('tool(currentTableFilter)', function (obj) {
                    var data = obj.data;
                    if (obj.event === 'edit') {
                        commodityId = obj.data.id;
                        var index = layer.open({
                            title: '编辑商品',
                            type: 2,
                            shade: 0.2,
                            maxmin: true,
                            shadeClose: true,
                            area: ['100%', '100%'],
                            content: '/CommodityInfo/UpdateCommodityInfoView',
                        });
                        $(window).on("resize", function () {
                            layer.full(index);
                        });
                        return false;
                    } else if (obj.event === 'delete') {
                        layer.confirm('真的删除行么', function (index) {
                            //发起请求，删除数据库中的用户
                            $.ajax({
                                url: 'http://localhost:31861/api/CommodityInfo/SoftDeleteCommodityInfo',
                                data: {
                                    'CommodityId': obj.data.id
                                },
                                type: 'post',
                                success: function (res) {
                                    if (res.isSuccess) {
                                        //刷新表格
                                        reLoadTable();
                                    }

                                    layer.msg(res.msg);
                                }
                            });
                            //obj.del();
                            layer.close(index);
                        });
                    } else if (obj.event === 'shoppingcart') {
                        commodityId = obj.data.id
                        var index = layer.open({
                            title: '购物车',
                            type: 2,
                            shade: 0.2,
                            maxmin: true,
                            area: ["500px", "300px"],
                            shadeClose: true,
                            content: '/ShoppingCart/AddShoppingCartView',
                        });
                        $(window).on("resize", function () {
                            layer.full(index);
                        });
                        return false;
                    } else if (obj.event === 'playcommodity') {
                        commodityId = obj.data.id
                        var index = layer.open({
                            title: '购买',
                            type: 2,
                            shade: 0.2,
                            maxmin: true,
                            area: ["500px", "300px"],
                            shadeClose: true,
                            content: '/CommodityInfo/PlayCommodityView',
                        });
                        $(window).on("resize", function () {
                            layer.full(index);
                        });
                        return false;
                    }



                });





            }



        });

        //刷新表格
        function reLoadTable() {
            tableIns.reload({
                //where: { //设定异步数据接口的额外参数，任意设
                //    aaaaaa: 'xxx'
                //    , bbb: 'yyy'
                //    //…
                //}
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        }
    </script>

</body>
</html>
